/*
 * Copyleft 2012 Power by Colen.
 *
 * Project: app-crm
 * Date: Apr 4, 2012
 */
package com.crm.business.web.cust.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.app.platform.base.utils.CommonUtil;
import com.app.platform.base.utils.DateUtil;
import com.app.platform.base.utils.StringUtil;
import com.crm.business.web.cust.service.CustomerService;
import com.crm.common.constant.CodeConst;
import com.crm.common.exception.CrmBusinessException;
import com.crm.common.web.service.impl.WebCommonServiceImpl;

/**
 * Customer Service implementations
 *
 * @author Colen
 *
 */
public class CustomerServiceImpl extends WebCommonServiceImpl implements
        CustomerService {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(CustomerServiceImpl.class);


    @Override
    public void doQuery(Map<String, Object> params) {
        // query
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_cust_customer_queryCustInfoByCond", params);

        LOG.info(list.size() + " customer(s) fetched!");
        if (list.size() == 0) {
            throw new CrmBusinessException("没有查询到任何记录！");
        }

        for (Map<String, Object> customer : list) {
            customer.put("gender_disp_nm", getCdNmByValue(CodeConst.CDD_CD0001, customer.get("gender")));
            customer.put("svc_locate_disp_nm", getCdNmByValue(CodeConst.CDD_CD0009, customer.get("svc_locate_id")));
            customer.put("car_brand_disp_nm", getCdNmByValue(CodeConst.CDD_CARBRAND, customer.get("car_brand")));
            customer.put("car_color_disp_nm", getCdNmByValue(CodeConst.CDD_CD0008, customer.get("car_color")));
            customer.put("car_size_type_disp_nm", getCdNmByValue(CodeConst.CDD_CD0002, customer.get("car_size_type")));
        }

        params.put("custInfos", list);
    }


    @Override
    public void doAdd(Map<String, Object> params) {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("cmobile", params.get("mobile"));

        // mobile
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_cust_customer_queryCustInfoByCond", args);
        if (!CommonUtil.isEmpty(list)) {
            throw new CrmBusinessException("手机号码已经存在！");
        }

        // car id
        args.clear();
        args.put("car_id", params.get("car_id"));
        list = getCommonDao().namingQueryForList("sql_cust_customer_checkCarId", args);
        if (!CommonUtil.isEmpty(list)) {
            throw new CrmBusinessException("车牌已经存在！");
        }

        // member id
        args.clear();
        String memberId = (String) params.get("member_id");
        args.put("member_id", params.get("member_id"));
        list = getCommonDao().namingQueryForList("sql_cust_customer_checkMemberId", args);
        if (!CommonUtil.isEmpty(list)) {
            throw new CrmBusinessException("会员号已经存在！");
        }

        
        args.clear();
        args.putAll(params);
        // create customer id
        String custId = super.nextSeq("seq_customer");
        args.put("cust_id", custId);
        args.put("reg_date", DateUtil.getCurrentDate());

        // add customer info.
        getCommonDao().namingUpdate("sql_cust_customer_addNewCustomer", args);

        // add member info.
        if (!StringUtil.isTrimEmpty(memberId)) {
            args.put("status", "0");
            args.put("balance", 0);
            getCommonDao().namingUpdate("sql_cust_addMemberInfo", args);
        }

        // add car info.
        args.put("owner_id", custId);
        args.put("owner_name", params.get("name"));
        getCommonDao().namingUpdate("sql_cust_addCarInfo", args);
    }


    @Override
    public void showEdit(Map<String, Object> params) {
        
        String custId = (String) params.get("custId");

        if (StringUtil.isTrimEmpty(custId)) {
            return;
        }

        Map<String, Object> search = new HashMap<String, Object>();
        search.put("cid", custId);
        
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_cust_customer_queryCustInfoByCond", search);
        if (CommonUtil.isEmpty(list)) {
            throw new CrmBusinessException("该记录可能已经被删除！");
        }

        Map<String, Object> custInfo = list.get(0);
        // store
        params.put("custInfo", custInfo);
    }


    @Override
    public void doEdit(Map<String, Object> params) {
        Map<String, Object> args = new HashMap<String, Object>();
        args.putAll(params);
        args.put("upd_date", DateUtil.getCurrentDate());
        getCommonDao().namingUpdate("sql_cust_customer_updateCustomer", args);
    }
}
